package top.as.sean.api.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.as.sean.biz.system.IRole;
import top.as.sean.common.response.Result;
import top.as.sean.dao.dto.system.DeleteDto;
import top.as.sean.dao.dto.system.RoleAdd;
import top.as.sean.dao.dto.system.RoleAuth;
import top.as.sean.dao.dto.system.RoleEdit;
import top.as.sean.dao.dto.system.RolePage;
import top.as.sean.dao.entity.pojo.system.Role;
import top.as.sean.dao.vo.system.RoleMenuVo;
import top.as.sean.dao.vo.system.RoleVo;

import java.util.List;

/**
 * 角色管理
 *
 * @author: ArchieSean
 * @create: 2023/1/3
 */
@RestController
@RequestMapping("role")
@RequiredArgsConstructor
public class RoleController {
    /**
     * 角色
     */
    private final IRole iRole;

    /**
     * 查询所有可用角色
     * @return list
     */
    @GetMapping("all")
    public Result<List<Role>> getAll(){
        return Result.success(iRole.getAll());
    }
    /**
     * 新增角色
     *
     * @return boolean
     */
    @PostMapping("add")
    public Result<Boolean> addRole(@Validated @RequestBody RoleAdd dto) {
        return Result.success(iRole.addRole(dto));
    }

    /**
     * 角色分页查询
     *
     * @param dto 入参
     * @return page
     */
    @GetMapping("page")
    public Result<Page<RoleVo>> getPage(@Validated @ModelAttribute RolePage dto) {
        return Result.success(iRole.getPage(dto));
    }

    /**
     * 编辑角色
     */
    @PutMapping("edit")
    public Result<Boolean> editRole(@Validated @RequestBody RoleEdit dto) {
        return Result.success(iRole.editRole(dto));
    }

    /**
     * 删除角色
     *
     * @param dto 角色id
     * @return boolean
     */
    @PostMapping("delete")
    public Result<Boolean> deleteRole(@Validated @RequestBody DeleteDto dto) {
        return Result.success(iRole.deleteRole(dto.getIds()));
    }

    /**
     * 角色管理-授权
     *
     * @param roleId 角色id
     * @return obj
     */
    @GetMapping("menu/{roleId}")
    public Result<RoleMenuVo> getMenuTree(@PathVariable("roleId") Long roleId) {
        return Result.success(iRole.getMenuTree(roleId));
    }

    /**
     * 角色-菜单 【授权】
     * @return boolean
     */
    @PostMapping("menu/authorization")
    public Result<Boolean> authorization(@Validated @RequestBody RoleAuth dto){
        return Result.success(iRole.authorization(dto));
    }

}
